<?php
namespace App\Admin\Controller;

use Illuminate\Routing\Controller;
use ModStart\Admin\Concern\HasAdminCRUD;
use ModStart\Detail\Detail;
use ModStart\Form\Form;
use ModStart\Grid\Grid;

class BlogController extends Controller
{
    use HasAdminCRUD;
    public function grid()
    {
        $grid = Grid::make('blog');
        $grid->canDelete(false);
        $grid->id('id', 'ID');
        $grid->text('title', '货币名称');
        $grid->image('cover', '封面图');
        $grid->image('icon', '图标');
        $grid->image('detail_img', '详情图');
//        $grid->text('summary', '兑换比例')->placeholder("1瓦努阿图币=***");
        $grid->text('content', '适用国家');
        $grid->text('unit', '单位');
        $grid->radio('state', '是否使用')->options(['0'=>'关闭','1'=>'使用']);;
        $grid->display('created_at', '创建时间');
        $this->pageTitle('货币管理');
        return $grid;
    }
    public function form()
    {
        $form = Form::make('blog');
        $form->text('title', '货币名称');
        $form->image('cover', '封面图');
        $form->image('icon', '图标');
        $form->image('detail_img', '详情图');
//        $form->text('summary', '兑换比例')->placeholder("1瓦努阿图币=***");;
        $form->text('content', '适用国家');
        $form->text('unit', '单位');
        $form->radio('state', '是否使用')->options(['0'=>'关闭','1'=>'使用']);
        $form->hookSaving(function (Form $form) {
            if ($form->isModeAdd()) {
                // 增加模式
                $data = $form->dataAdding();
                if(substr($data['detail_img'],0,4) != 'http'){
                    $data['detail_img'] = getenv("host_url").$data['detail_img'];
                }
                if(substr($data['icon'],0,4) != 'http'){
                    $data['icon'] = getenv("host_url").$data['icon'];
                }
                
                if(substr($data['cover'],0,4) != 'http'){
                    $data['cover'] = getenv("host_url").$data['cover'];
                }
                //$data['xxx'] = 'xxx';
                // 替换入库数据
                $form->dataAdding($data);
                 } else if ($form->isModeEdit()) {
                // 修改
                $data = $form->dataEditing();
                if(substr($data['detail_img'],0,4) != 'http'){
                    $data['detail_img'] = getenv("host_url").$data['detail_img'];
                }
                if(substr($data['icon'],0,4) != 'http'){
                    $data['icon'] = getenv("host_url").$data['icon'];
                }
                
                if(substr($data['cover'],0,4) != 'http'){
                    $data['cover'] = getenv("host_url").$data['cover'];
                }
                // 替换入库数据
                $form->dataEditing($data);
             }
        });
        
        return $form;
    }
    public function detail()
    {
        $detail = Detail::make('blog');
        $detail->id('id', 'ID');
        $detail->display('created_at', '创建时间');
        $detail->text('title', '货币名称');
        $detail->image('cover', '封面图');
        $detail->image('icon', '图标');
        $detail->image('detail_img', '详情图');
//        $detail->text('summary', '兑换比例')->placeholder("1瓦努阿图币=***");
        $detail->text('content', '适用国家');
        $detail->radio('content', '是否使用')->options(['0'=>'关闭','1'=>'使用']);
        $detail->text('unit', '单位');
        return $detail;
    }
}